{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_user_click(rating_file):\n",
    "    \"\"\"\n",
    "    get user clcik list\n",
    "    Args:\n",
    "        rating_file:input_file\n",
    "    Return:\n",
    "        dict,key:userid,value:[item1,item2]\n",
    "    \"\"\"\n",
    "    if not os.path.exists(rating_file):\n",
    "        return {}\n",
    "    df = pd.read_csv(rating_file,delimiter=\"::\",header=None)\n",
    "    df.column=[\"uid\",\"mid\",\"rating\",\"time\"]\n",
    "    print(df[0])\n",
    "    df = df.loc[df.rating>=4]\n",
    "    user_click = {}\n",
    "    print(len(df))\n",
    "    for i in range(0,200):\n",
    "        uid = df.iloc[i]['uid']\n",
    "        mid = df.iloc[i]['mid']\n",
    "        if uid not in user_click:\n",
    "            user_click[uid] = [];\n",
    "        user_click[uid].append(mid)\n",
    "    return user_click"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'1'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: '1'"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "df =  pd.read_csv(\"ml-1m/ratings.dat\",header=None,delimiter=\"::\",usecols=[0,1,2])\n",
    "df = df.loc[df[2]>3]\n",
    "user_click = {}\n",
    "for index, row in df.iterrows():\n",
    "    if index >2000:\n",
    "        break\n",
    "    uid = row[0]\n",
    "    mid = row[1]\n",
    "    if uid not in user_click:\n",
    "        user_click[uid] = []\n",
    "    user_click[uid].append(mid)\n",
    "print(user_click[\"1\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
